package oldVersion.server;

/**
 *@author Adam
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketConnect {

	private int my_backlog = 5;
	private ServerSocket my_serverSocket;
	public SocketConnect(int a_port) {
		try {
			my_serverSocket = new ServerSocket(a_port, my_backlog);
			System.out.println("TCP socket listening on port " + a_port);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (SecurityException se) {
			se.printStackTrace();
		}
	}

	public void listen() {
		while (true) {
			try {
				Socket socket = my_serverSocket.accept();
				BufferedReader in = new BufferedReader(new InputStreamReader(socket
						.getInputStream()));
				String msg = in.readLine();
				System.out.println("Recceived message: " + msg);
				PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
				String response = parse(msg);
				out.println(response);
				in.close();
				socket.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			} catch (SecurityException se) {
				se.printStackTrace();
			}
		}
	}

	public String parse(String incoming){
		String location = "";
		if (incoming.equals("<RMIServer>where</RMIServer>"));
		try{
			InetAddress thisIp =InetAddress.getLocalHost();
			location = thisIp.getHostAddress();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return location;
	}
} 

